package com.Txunda.fxw.ui.activity.home;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.Txunda.fxw.R;
import com.Txunda.fxw.base.utils.util.JSONUtils;
import com.Txunda.fxw.base.utils.util.ListUtils;
import com.Txunda.fxw.base.utils.util.MapUtils;
import com.Txunda.fxw.ui.adapter.SmallSizedAdapter;
import com.Txunda.fxw.ui.adapter.SmallSizedClassifyAdapter;
import com.Txunda.fxw.ui.base.BaseAty;
import com.Txunda.fxw.ui.http.Company;
import com.Txunda.fxw.ui.http.Index;
import com.Txunda.fxw.ui.listener.AdapterViewClickListener;
import com.Txunda.fxw.ui.toolkit.util.PreferenceUtil;
import com.itheima.pulltorefreshlib.PullToRefreshBase;
import com.itheima.pulltorefreshlib.PullToRefreshListView;

import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * 创建时间：2018/11/15 15:34
 * 作    者：Hzj
 * 描    述：小影
 * 注    意：
 * 修    改：
 * 参    考：
 * 出    自：书山府科技 android
 **/
public class SmallSizedAty extends BaseAty {

    @BindView(R.id.aty_title3_icon)
    ImageView atyTitle3Icon;
    @BindView(R.id.aty_title3_tv_center)
    TextView atyTitle3TvCenter;
    @BindView(R.id.aty_title3_tv_right2)
    TextView atyTitle3TvRight2;
    @BindView(R.id.top_classfy_rv)
    RecyclerView topClassfyRv;
    @BindView(R.id.small_sized_ptr_lv)
    PullToRefreshListView smallSizedPtrLv;
    @BindView(R.id.no_data_iv)
    ImageView noDataIv;
    @BindView(R.id.drawer_layout)
    LinearLayout drawerLayout;
    private Index index;
    private String area_name = "";

    private SmallSizedClassifyAdapter adapter;

    private SmallSizedAdapter smallSizedAdapter;
    private Company company;
    private int p = 1;
    private String lat = "";
    private String lnt = "";
    private String classId = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        atyTitle3TvCenter.setText("峰行小影");
        atyTitle3TvRight2.setVisibility(View.VISIBLE);
        atyTitle3TvRight2.setText(area_name);
        initView();
    }

    // 初始化控件
    private void initView() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        topClassfyRv.setLayoutManager(manager);
        topClassfyRv.setAdapter(adapter);

        smallSizedPtrLv.setEmptyView(noDataIv);
        smallSizedPtrLv.setAdapter(smallSizedAdapter);

        smallSizedPtrLv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                p = 1;
                getCompanyList();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                p++;
                getCompanyList();
            }
        });

        smallSizedPtrLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                int pos = i - 1;
                String url = smallSizedAdapter.getItem(pos).get("url");
                String title = smallSizedAdapter.getItem(pos).get("name");
                String com_id = smallSizedAdapter.getItem(pos).get("com_id");
                Bundle bundle = new Bundle();
                bundle.putString("url", url);
                bundle.putString("title", title);
                bundle.putString("com_id", com_id);
                startActivity(VideoPlayAty.class, bundle);
            }
        });
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_small_sized;
    }

    @Override
    protected void initialized() {
        index = new Index();
        company = new Company();
        area_name = PreferenceUtil.readString(getApplicationContext(), PreferenceUtil.sp_mode);
        adapter = new SmallSizedClassifyAdapter(this);

        smallSizedAdapter = new SmallSizedAdapter(this);
        // 获取经度纬度
        lat = PreferenceUtil.readString(this, PreferenceUtil.sp_lat);
        lnt = PreferenceUtil.readString(this, PreferenceUtil.sp_lnt);

    }

    @Override
    protected void requestData() {
        showProgressDialog();
        index.fxMovie(this);
    }

    /**
     * 获取商家列表
     */
    private void getCompanyList() {
        String district_id = "";
        String city_id = "";
        String province_id = "";
        company.companyList(lat, lnt, p, classId, district_id, city_id, province_id, this);
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
        if (MapUtils.isEmpty(map))
            return;
        if (requestUrl.contains("Api/Index/fxmovie")) {
            if (smallSizedClassify(map)) return;
            return;
        }
        if (requestUrl.contains("companyList")) { // 商家列表
            Log.d("jjjjjj", "onComplete: ========"+requestUrl);
            companyListData(map);
        }
    }

    // 小影分类
    private boolean smallSizedClassify(Map<String, String> map) {
        List<Map<String, String>> smallSized = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
        if (ListUtils.isEmpty(smallSized))
            return true;
        adapter.update(smallSized);
        p = 1;
        classId = smallSized.get(0).get("id");
        getCompanyList();
        adapter.setAdapterViewClickListener(new AdapterViewClickListener<Map<String, String>>() {
            @Override
            public void onViewClick(View view, int position, Map<String, String> item) {
                p = 1;
                classId = item.get("id");
                getCompanyList();
                adapter.setSelectedPosition(position);
            }
        });
        return false;
    }

    // 商家列表
    private void companyListData(Map<String, String> map) {
        List<Map<String, String>> company_list_data = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
        if (p == 1)
            smallSizedAdapter.clear();
        if (!ListUtils.isEmpty(company_list_data))
            smallSizedAdapter.updata(company_list_data);
        smallSizedPtrLv.onRefreshComplete();
    }

}
